<?php


namespace app\crontab\controller\once;


use custom\Log;
use custom\TencentMap;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Exception;

/**
 * 百度经纬度转腾讯经纬度
 * Class BaiduLocation2TenXunLocation
 * @package app\crontab\controller
 */
class BaiduLocation2TenXunLocation extends Command {

    protected function configure() {
        $this->setName('BaiduLocation2TenXunLocation');
    }

    protected function execute(Input $input, Output $output) {
        try {
            set_time_limit(0);
            echo "start\n";
            self::region();
            self::house();
            self::house_village();
            self::business_area();
            self::metro();
            echo "end\n";
        } catch (Exception $e) {
            $err = "异常信息:{$e->getMessage()} | 异常所在的代码行:{$e->getFile()} | 异常文件{$e->getFile()} | php 版本 " . PHP_VERSION . "(" . PHP_OS . ")";
            Log::write($err, "BaiduLocation2TenXunLocationErr.log");
        }
    }

    protected function house() {
        $list = db('house')
            ->field('lng,lat,id')
            ->select();
        foreach ($list as $item) {
            $data = [
                'locations' => "{$item['lat']},{$item['lng']}",
                'type'      => 3,
            ];
            $res = self::common($data);
            $update = [
                'tx_lng'      => $res['locations'][0]['lng'],
                'tx_lat'      => $res['locations'][0]['lat'],
                'tx_location' => Db::raw("POINT('{$res['locations'][0]['lng']}', '{$res['locations'][0]['lat']}')")
            ];
            db('house')->where('id', $item['id'])->update($update);
        }
    }

    protected function house_village() {
        $list = db('house_village')
            ->field('lng,lat,id')
            ->select();
        foreach ($list as $item) {
            $data = [
                'locations' => "{$item['lat']},{$item['lng']}",
                'type'      => 3,
            ];
            $res = self::common($data);
            $update = [
                'tx_lng'      => $res['locations'][0]['lng'],
                'tx_lat'      => $res['locations'][0]['lat'],
                'tx_location' => Db::raw("POINT('{$res['locations'][0]['lng']}', '{$res['locations'][0]['lat']}')")
            ];
            db('house_village')->where('id', $item['id'])->update($update);
        }
    }

    protected function metro() {
        $list = db('metro')
            ->field('lng,lat,id')
            ->select();
        foreach ($list as $item) {
            $data = [
                'locations' => "{$item['lat']},{$item['lng']}",
                'type'      => 3,
            ];
            $res = self::common($data);
            $update = [
                'tx_lng'      => $res['locations'][0]['lng'],
                'tx_lat'      => $res['locations'][0]['lat'],
                'tx_location' => Db::raw("POINT('{$res['locations'][0]['lng']}', '{$res['locations'][0]['lat']}')")
            ];
            db('metro')->where('id', $item['id'])->update($update);
        }
    }

    protected function business_area() {
        $list = db('business_area')
            ->field('lng,lat,id')
            ->select();
        foreach ($list as $item) {
            $data = [
                'locations' => "{$item['lat']},{$item['lng']}",
                'type'      => 3,
            ];
            $res = self::common($data);
            $update = [
                'tx_lng'      => $res['locations'][0]['lng'],
                'tx_lat'      => $res['locations'][0]['lat'],
                'tx_location' => Db::raw("POINT('{$res['locations'][0]['lng']}', '{$res['locations'][0]['lat']}')")
            ];
            db('business_area')->where('id', $item['id'])->update($update);
        }
    }

    protected function region() {
        $list = db('region')
            ->where('rank', '<=', 3)
            ->field('x(location) as lng,Y(location) as lat,id')
            ->limit(1)
            ->select();
        foreach ($list as $item) {
            $data = [
                'locations' => "{$item['lat']},{$item['lng']}",
                'type'      => 3,
            ];
            $res = self::common($data);
            $update = [
//                    'tx_lng'      => $res['locations'][0]['lng'],
//                    'tx_lat'      => $res['locations'][0]['lat'],
'tx_location' => Db::raw("POINT('{$res['locations'][0]['lng']}', '{$res['locations'][0]['lat']}')")
            ];
            db('region')->where('id', $item['id'])->update($update);
        }
    }

    public static function common($data) {
        $Map = TencentMap::getInstance();
        while (true) {
            $Map->BaiduLocation2TenXunLocation($data, $res);
            if ($res === -1) {
                throw new Exception('地图调用失败');
            } elseif ($res['status'] != 0) {
                if ($res['status'] == 120) {
                    sleep(1.5);
                    continue;
                } else {
                    throw new Exception($res['message']);
                }
            } elseif ($res['status'] == 0) {
                break;
            }
        }
        return $res;
    }

}